package com.ruoyi.system.service.impl;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.vo.LearningRecordVo;
import com.ruoyi.system.mapper.ZtfStudyRecordMapper;
import com.ruoyi.system.service.ZtfStudyRecordService;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

@Service
public class ZtfStudyRecordServiceImpl implements ZtfStudyRecordService {

    @Autowired
    private ZtfStudyRecordMapper ztfStudyRecordMapper;
    /**
     * 统计观看时长
     * @param begin
     * @param end
     * @return
     */
    @Override
    public LearningRecordVo getviewStatistics(LocalDate begin, LocalDate end,Long userId) throws ParseException {
        //用来添加天数
        List<LocalDate> dateList=new ArrayList<>();
        dateList.add(begin);
        while (!begin.equals(end)){
            begin=begin.plusDays(1);
            dateList.add(begin);
        }
        List<Integer> viewList=new ArrayList<>();
        LocalDateTime beginTime=null;
        //定义一个集合用来存储观看时长
        for (LocalDate date : dateList) {
             beginTime = LocalDateTime.of(date, LocalTime.MIN);
            LocalDateTime endTime = LocalDateTime.of(date, LocalTime.MAX);
            Map map = new HashMap();
            map.put("begin", beginTime);
            map.put("end",endTime);
            map.put("userId",userId);
            //计算一天的观看时长
            Integer minutes=ztfStudyRecordMapper.countByMap(map);
            minutes=minutes==null ? 0 :minutes;
           viewList.add(minutes);
        }
        //格式化日期
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        //获取当天日期
        String toDayEndTime = LocalDate.now().format(formatter);
        LocalDate date1= LocalDate.parse(toDayEndTime,formatter);
        LocalDateTime todayBeginTime = LocalDateTime.of(date1, LocalTime.MIN);
        LocalDateTime todayEndTime = LocalDateTime.of(date1, LocalTime.now());

        //统计今日学习时长
        Map map = new HashMap();
        map.put("begin", todayBeginTime);
        map.put("end",todayEndTime);
        map.put("userId",userId);

        //统计今日学习时长
        Integer todayLearnTime=ztfStudyRecordMapper.countByMap(map);
        todayLearnTime=todayLearnTime==null ? 0 :todayLearnTime;

        //今日学习时长转换
        int todayHours = todayLearnTime / 60;  // 计算小时数（整数部分）
        int todayMinutes = todayLearnTime % 60;//计算剩余的分钟数
        String todayLearnTimeStr= todayHours + " 小时 " + todayMinutes + " 分钟";

        Double todayAllTime = todayLearnTime / 60.0;
        String todayAllTimeStr = String.format("%.2f", todayAllTime);
        todayAllTimeStr=todayAllTimeStr.equals("0.0小时") ? "0小时": todayAllTimeStr+"小时";
        //求累计学习时长
        int totalLearnTime = viewList.stream().mapToInt(Integer::intValue).sum();
        //累计学习时间转换
        int hours = totalLearnTime / 60;  // 计算小时数（整数部分）
        int minutes = totalLearnTime % 60;//计算剩余的分钟数
        String totalLearnTimeStr= hours + " 小时 " + minutes + " 分钟";

        return LearningRecordVo
                .builder()
                .dateList(StringUtils.join(dateList, ","))
                .viewList(StringUtils.join(viewList, ","))
                .todayAllTimeStr(todayAllTimeStr)
                .todayLearnTimeStr(todayLearnTimeStr)
                .totalTimeStr(totalLearnTimeStr)
                .build();
    }
}
